package com.web;

import com.pojo.Zixunshi_lfy;
import com.service.Fee_lfyService;
import com.service.Zixunshi_lfyService;
import com.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@CrossOrigin(origins = "http://localhost:8081")
@RequestMapping("/zixunshi")
@RestController
public class Zixunshi_lfyController {
    @Autowired
    private Zixunshi_lfyService zService;
    @Autowired
    private Fee_lfyService fService;
    @RequestMapping("/name")
    public ResultEntity selectName() {

        return ResultEntity.ok(zService.selectName(6));
    }

    @RequestMapping("/city")
    public ResultEntity selectCityByName( Integer id) {
        List<Zixunshi_lfy> list=new ArrayList<>();

        Zixunshi_lfy zixunshiLfy = zService.selectCityByName(id);

        List<Zixunshi_lfy> zixunshiLfies = zService.selectAdept(id);
        HashSet<Zixunshi_lfy> unique=new
                HashSet<>(zixunshiLfies);
        List<Zixunshi_lfy> uniqueList = new ArrayList<>(unique);

        List<Zixunshi_lfy> list1 = fService.selectType(id);
        HashSet<Zixunshi_lfy> unique1=new
                HashSet<>(list1);
        List<Zixunshi_lfy> uniqueList1 = new ArrayList<>(unique1);

        list.add(zixunshiLfy);
        list.addAll(uniqueList);
        list.addAll(uniqueList1);
        return ResultEntity.ok(list);

    }

    @RequestMapping("/all")
    public ResultEntity selectAll(@RequestParam("gradeName") String gradeName) {
        List<Zixunshi_lfy> list=new ArrayList<>();
        List<Zixunshi_lfy> list1=zService.selectAll(gradeName);
        HashSet<Zixunshi_lfy> unique=new
                HashSet<>(list1);
        list.addAll(new ArrayList<>(unique));

        return ResultEntity.ok(list);
    }

    @RequestMapping("/all1")
    public ResultEntity selectAll1(@RequestParam("gradeName") String gradeName,
                                   @RequestParam("address") String address,
                                   @RequestParam("sex") String sex,
                                   @RequestParam("type") List<String> type,
                                   @RequestParam("adeptName") List<String> adeptName) {
        List<Zixunshi_lfy> list=new ArrayList<>();

        List<Zixunshi_lfy> list1=zService.selectAll1(gradeName,address,sex,type,adeptName);

        HashSet<Zixunshi_lfy> unique=new
                HashSet<>(list1);
        list.addAll(new ArrayList<>(unique));
//        for(int i=0;i<list.size();i++){
//            list.get(i).getAdeptLfyList().stream()
//                    .collect(Collectors.collectingAndThen(
//                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Adept_lfy::getAdeptName))),
//                            ArrayList::new));
//            list.get(i).getFeeLfyList().stream()
//                    .collect(Collectors.collectingAndThen(
//                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Fee_lfy::getFeeType))),
//                            ArrayList::new));
//        }
        return ResultEntity.ok(list);
    }

    @RequestMapping("/list")
    public ResultEntity searchAll(){
        List<Zixunshi_lfy> list=new ArrayList<>();
        List<Zixunshi_lfy> list1=zService.searchAll();
        HashSet<Zixunshi_lfy> unique=new HashSet<>(list1);
        list.addAll(unique);
        return ResultEntity.ok("400",list);
    }
    @RequestMapping("/searchName")
    public ResultEntity selectAllByName(@RequestParam("name") String zixunshiName){
        List<Zixunshi_lfy> list=new ArrayList<>();
        List<Zixunshi_lfy> list1=zService.selectAllByName(zixunshiName);
        HashSet<Zixunshi_lfy> unique=new HashSet<>(list1);
        list.addAll(unique);
        return ResultEntity.ok(list);
    }

    @RequestMapping("/delete")
    public ResultEntity delete(@RequestParam("id") Integer zixunshiId){
        return ResultEntity.ok(zService.delete(zixunshiId));
    }


    @RequestMapping("/insert")
    public ResultEntity insertInfo(Zixunshi_lfy zixunshi_lfy){
        return ResultEntity.ok(zService.insertInfo(zixunshi_lfy));
    }

    @RequestMapping("/update")
    public ResultEntity updateInfo( Zixunshi_lfy zixunshi_lfy){
        return ResultEntity.ok(zService.updateInfo(zixunshi_lfy));
    }
}
